<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Menu extends Model
{
    //
    protected $table = 'menus';
    protected $fillable = [
        'title', 'path', 'p_id', 'icon','level'
    ];

    /**
     * 模糊查询
     * @param $query
     * @param $name
     */
    public function scopeName($query, $name)
    {

        if ($name) {
            $query->where('title', 'like', '%' . $name . '%');
        }
    }


    /**
     *新增
     * @param array $data
     * @return bool
     */
    static public function add($data = [])
    {
        $re = self::create($data);
        if ($re) {
            return true;
        }
        return false;
    }


    /**
     * 编辑
     * @param $data
     * @return bool
     */
    static public function edit($data)
    {
       $menu =  self::find($data['menu_id']);
        $menu->title = $data['title'];
        $menu->path = $data['path'];
        $menu->p_id = $data['p_id'];
//        $menu->icon = $data['icon'];
        $menu->level = $data['level'];
        if($menu->save()){
            return true;
        }
        return false;
    }
}
